package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.*;
import cc.rengu.igas.momp.common.dao.impl.*;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.bean.*;
import cc.rengu.igas.momp.facade.request.QueryMcListWithDetialInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryMcListWithDetialInfoResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卡券类营销活动列表查询(包含具体活动的详情)
 *
 * @author liujinan
 * @date 2021/1/11 10:39
 */
public class QueryMcListWithDetialInfoService extends RadpService {

    private String globalInstId;
    private String queryMcNo;
    private String queryMcNanme;
    private String queryMcStatus;
    private String queryAuditStatus;
    private String queryOrgId;
    private String queryType;
    private String queryMcType;
    private String globalChannelId;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryMcListWithDetialInfoRequest queryMcListWithDetialInfoRequest = new QueryMcListWithDetialInfoRequest();
            ConvertUtil.convertOutput(queryMcListWithDetialInfoRequest);

            QueryMcListWithDetialInfoResponse queryMcListWithDetialInfoResponse = new QueryMcListWithDetialInfoResponse();
            queryMcListWithDetialInfoResponse.setHeader(queryMcListWithDetialInfoRequest.getHeader());
            BizResponse<QueryMcListWithDetialInfoResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryMcListWithDetialInfoResponse);

            /* 请求报文检查 */
            messageValidation(queryMcListWithDetialInfoRequest);

            //营销活动查询
            BizResponse<QueryMcListWithDetialInfoResponse> bizResponseTmp = queryMcListWithDetialInfo(queryMcListWithDetialInfoRequest, queryMcListWithDetialInfoResponse);

            ConvertUtil.convertInput(bizResponseTmp.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 卡券类营销活动列表查询
     *
     * @param queryMcListWithDetialInfoRequest  卡券类营销活动列表查询接口请求对象
     * @param queryMcListWithDetialInfoResponse 卡券类营销活动列表查询接口应答对象
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryMcListWithDetialInfoResponse> 卡券类营销活动列表查询接口应答对象
     * @throws BizException 异常信息
     * @author liujinan
     * @date 2021/1/11 11:57
     */
    private BizResponse<QueryMcListWithDetialInfoResponse> queryMcListWithDetialInfo(QueryMcListWithDetialInfoRequest queryMcListWithDetialInfoRequest, QueryMcListWithDetialInfoResponse queryMcListWithDetialInfoResponse) throws Exception {

        BizResponse<QueryMcListWithDetialInfoResponse> bizResponse = new BizResponse<>();
        List<McListWithDetialInfoBean> mcListWithDetialInfoResultList = new ArrayList<>();

        /* 查询营销活动列表数据 */
        List<McListWithDetialInfoBean> mcListWithDetialInfoBeanList = selectMcListWithDetialInfo();

        if (null == mcListWithDetialInfoBeanList || mcListWithDetialInfoBeanList.isEmpty()) {
            rglog.info("根据查询条件查询到0条卡券类营销活动列表!");
            queryMcListWithDetialInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
            queryMcListWithDetialInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);
        } else {
            //判断机构号
            if (!StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getOrgId())) {
                McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
                try {
                    List<TMMcOrgInfo> tmMcOrgInfoList = mcOrgInfoMapper.selectMcOrgInfoListByinstIdOrgId(queryMcListWithDetialInfoRequest.getHeader().getInstId(), queryMcListWithDetialInfoRequest.getOrgId());
                    for (TMMcOrgInfo tmMcOrgInfo : tmMcOrgInfoList) {
                        List<McListWithDetialInfoBean> mcListInfoByorgIdList1 = mcListWithDetialInfoBeanList.stream().filter(item -> item.getMcInfoObj().getMcNo().equals(tmMcOrgInfo.getMcNo())).collect(Collectors.toList());
                        mcListWithDetialInfoResultList.addAll(mcListInfoByorgIdList1);
                    }
                } catch (Exception e) {
                    rglog.error(e.getMessage());
                }
            } else {
                mcListWithDetialInfoResultList.addAll(mcListWithDetialInfoBeanList);
            }

            if (mcListWithDetialInfoResultList.isEmpty()) {
                rglog.info("经过机构<{}>筛选后的卡券类营销活动列表为空!", queryMcListWithDetialInfoRequest.getOrgId());
                queryMcListWithDetialInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryMcListWithDetialInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);
            } else {
                //查询各营销活动明细信息
                queryCouponMcDetailInfo(mcListWithDetialInfoResultList);

                queryMcListWithDetialInfoResponse.setTotalRows(String.valueOf(mcListWithDetialInfoResultList.size()));
                if (queryMcListWithDetialInfoRequest.getPageIndex().equals("-1") || queryMcListWithDetialInfoRequest.getPageSize().equals("-1")) {
                    queryMcListWithDetialInfoResponse.setTotalPage(CommonConstant.MINUS_ONE_COMMON_CONSTANT);
                    queryMcListWithDetialInfoResponse.setPageIndex(CommonConstant.MINUS_ONE_COMMON_CONSTANT);
                    queryMcListWithDetialInfoResponse.setPageSize(String.valueOf(mcListWithDetialInfoResultList.size()));
                    //查询到的链表信息
                    queryMcListWithDetialInfoResponse.setMcInfoList(mcListWithDetialInfoResultList);
                } else {
                    if (CommonConstant.ZERO == (mcListWithDetialInfoResultList.size() % Integer.parseInt(queryMcListWithDetialInfoRequest.getPageSize()))) {
                        queryMcListWithDetialInfoResponse.setTotalPage(String.valueOf(mcListWithDetialInfoResultList.size() / Math.abs(Integer.parseInt(queryMcListWithDetialInfoRequest.getPageSize()))));
                    } else {
                        queryMcListWithDetialInfoResponse.setTotalPage(String.valueOf(mcListWithDetialInfoResultList.size() / Math.abs(Integer.parseInt(queryMcListWithDetialInfoRequest.getPageSize())) + 1));
                    }
                    /* 获取所有活动信息 */
                    if (CommonConstant.NO_LIMIT_COMMON_CONSTANT.equals(queryMcListWithDetialInfoRequest.getPageIndex()) || CommonConstant.NO_LIMIT_COMMON_CONSTANT.equals(queryMcListWithDetialInfoRequest.getPageSize())) {
                        /* 拼接交易信息 */
                        mcListWithDetialInfoResultList.sort(Comparator.comparingInt(x -> Integer.parseInt(x.getMcInfoObj().getCreateTime())));
                        queryMcListWithDetialInfoResponse.setMcInfoList(mcListWithDetialInfoResultList);
                    } else {
                        /* 分页查询 */
                        int fromIndex = Integer.parseInt(queryMcListWithDetialInfoRequest.getPageIndex()) * Integer.parseInt(queryMcListWithDetialInfoRequest.getPageSize());
                        int endIndex = fromIndex + Integer.parseInt(queryMcListWithDetialInfoRequest.getPageSize());
                        if (fromIndex <= mcListWithDetialInfoResultList.size()) {
                            List<McListWithDetialInfoBean> mcListInfoBeanList1 = mcListWithDetialInfoResultList.subList(fromIndex, Math.min(endIndex, mcListWithDetialInfoResultList.size()));
                            mcListInfoBeanList1.sort(Comparator.comparingInt(x -> Integer.parseInt(x.getMcInfoObj().getCreateTime())));
                            queryMcListWithDetialInfoResponse.setMcInfoList(mcListWithDetialInfoResultList);
                        }
                    }
                }
            }
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryMcListWithDetialInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryMcListWithDetialInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        queryMcListWithDetialInfoResponse.setPageIndex(queryMcListWithDetialInfoRequest.getPageIndex());
        queryMcListWithDetialInfoResponse.setPageSize(queryMcListWithDetialInfoRequest.getPageSize());
        bizResponse.setResult(queryMcListWithDetialInfoResponse);

        return bizResponse;
    }

    /**
     * 商户查询营销活动详细信息
     *
     * @param mcListWithDetialInfoResultList 卡券类活动详情查询接口应答报文
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/1/13 19:23
     */
    private void queryCouponMcDetailInfo(List<McListWithDetialInfoBean> mcListWithDetialInfoResultList) throws Exception {

        for (McListWithDetialInfoBean mcListWithDetialInfoBean : mcListWithDetialInfoResultList) {

            String mcNo = mcListWithDetialInfoBean.getMcInfoObj().getMcNo();

            /* 查询营销活动机构信息 */
            List<TMMcOrgInfo> mcOrgInfoList = getAllMcOrgListInfo(mcNo);
            if (null == mcOrgInfoList || mcOrgInfoList.isEmpty()) {
                rglog.error("机构<{}>的卡券类营销活动<{}>的机构信息查询失败!", globalInstId, mcNo);
                throw new BizException(RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespDesc());
            }

//            /* 查询营销活动商户信息 */
//            List<Object> mcMchntInfoList = getAllMcMchntListInfo(mcNo);
//            if (mcMchntInfoList.isEmpty()) {
//                rglog.error("机构<{}>的卡券类营销活动<{}>的商户信息查询失败!", globalInstId, mcNo);
//                throw new BizException(RespCodeEnum.GET_MC_MCHNT_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_MCHNT_INFO_ERROR.getRespDesc());
//            }

            /* 查询营销活动用户信息 */
            List<TMMcUserInfo> mcUserInfoList = null;
            if (CommonConstant.SPECIFIC_CLIENT_SUPPORT.equals(mcListWithDetialInfoBean.getMcInfoObj().getClientRange())) {
                mcUserInfoList = getAllMcUserListInfo(mcNo);
                if (null == mcUserInfoList || mcUserInfoList.isEmpty()) {
                    rglog.error("机构<{}>的卡券类营销活动<{}>的用户信息查询失败!", globalInstId, mcNo);
                    throw new BizException(RespCodeEnum.GET_MC_USER_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_USER_INFO_ERROR.getRespDesc());
                }
            }

            /* 查询营销活动时间信息 */
            List<TMMcTimeInfo> mcTimeInfoList = null;
            if (CommonConstant.SPECIFIC_TIME_SUPPORT.equals(mcListWithDetialInfoBean.getMcInfoObj().getMcTimeFlag())) {
                mcTimeInfoList = getAllMcTimeListInfo(mcNo);
                if (null == mcTimeInfoList || mcTimeInfoList.isEmpty()) {
                    rglog.error("机构<{}>的卡券类营销活动<{}>的时间信息查询失败!", globalInstId, mcNo);
                    throw new BizException(RespCodeEnum.GET_MC_TIME_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_TIME_INFO_ERROR.getRespDesc());
                }
            }

            /* 查询营销活动规则信息 */
            List<TMMcCouponRuleInfo> mcCouponRuleInfoList = getAllMcRuleListInfo(mcNo);
            if (null == mcCouponRuleInfoList || mcCouponRuleInfoList.isEmpty()) {
                rglog.error("机构<{}>的卡券类营销活动<{}>的卡券类规则查询失败!", globalInstId, mcNo);
                throw new BizException(RespCodeEnum.GET_MC_RULE_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_RULE_INFO_ERROR.getRespDesc());
            }

//            /* 查询营销活动支付产品信息 */
//            List<TMMcProductInfo> mcProductInfoList = getAllMcProductListInfo(mcNo);
//            if (null == mcProductInfoList || mcProductInfoList.isEmpty()) {
//                rglog.error("机构<{}>的卡券类营销活动<{}>的支付产品信息查询失败!", globalInstId, mcNo);
//                throw new BizException(RespCodeEnum.GET_MC_PRODUCT_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_PRODUCT_INFO_ERROR.getRespDesc());
//            }

            /* 转换数据库信息 */
            List<McOrgInfoBean> mcOrgInfoBeanList = mcOrgInfoList.stream().map(this::convertMcOrgInfo).collect(Collectors.toList());
            mcListWithDetialInfoBean.setOrgInfoList(mcOrgInfoBeanList);

//            List<McMchntInfoBean> mcMchntInfoBeanList = mcMchntInfoList.stream().map(this::convertMcMchntInfo).collect(Collectors.toList());
//            mcListWithDetialInfoBean.setMchntInfoList(mcMchntInfoBeanList);

            if (CommonConstant.SPECIFIC_CLIENT_SUPPORT.equals(mcListWithDetialInfoBean.getMcInfoObj().getClientRange())) {
                if (mcUserInfoList != null) {
                    List<McUserInfoBean> mcUserInfoBeanList = mcUserInfoList.stream().map(this::convertMcUserInfo).collect(Collectors.toList());
                    mcListWithDetialInfoBean.setUserInfoList(mcUserInfoBeanList);
                }
            }

            if (mcTimeInfoList != null && CommonConstant.SPECIFIC_TIME_SUPPORT.equals(mcListWithDetialInfoBean.getMcInfoObj().getMcTimeFlag())) {
                List<McTimeInfoBean> mcTimeInfoBeanList = mcTimeInfoList.stream().map(this::convertMcTimeInfo).collect(Collectors.toList());
                mcListWithDetialInfoBean.setTimeInfoList(mcTimeInfoBeanList);
            }

            List<CouponRuleInfoBean> mcCouponRuleInfoBean = mcCouponRuleInfoList.stream().map(this::convertMcRuleInfo).collect(Collectors.toList());
            mcListWithDetialInfoBean.setRuleInfoList(mcCouponRuleInfoBean);

//            List<McProductInfoBean> mcProductInfoBeanList = mcProductInfoList.stream().map(this::convertMcProductInfo).collect(Collectors.toList());
//            mcListWithDetialInfoBean.setProductInfoList(mcProductInfoBeanList);
        }
    }

    /**
     * 查询营销活动列表数据
     *
     * @author liujinan
     * @date 2021/1/11 20:49
     */
    private List<McListWithDetialInfoBean> selectMcListWithDetialInfo() {

        List<McListWithDetialInfoBean> mcListInfoBeanList = new ArrayList<>();
        try {
            McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
            /* 查询卡券类营销活动数据 */
            List<TMMcCouponInfo> tMMcCouponInfoList = mcInfoListMapper.selectCouponMcListInfoWithChannelId(globalInstId, queryMcNo, queryMcNanme, queryMcType, globalChannelId, queryMcStatus, queryAuditStatus, queryOrgId);
            if (null != tMMcCouponInfoList) {
                mcListInfoBeanList = tMMcCouponInfoList.stream().map(this::convertCouponMcListInfo).collect(Collectors.toList());
            }
        } catch (Exception e) {
            rglog.error("获取卡券类营销活动列表失败!");
            throw new BizException(RespCodeEnum.GET_MC_LIST_ERROR.getRespCode(), RespCodeEnum.GET_MC_LIST_ERROR.getRespDesc());
        }

        return mcListInfoBeanList;
    }


    /**
     * 卡券类营销活动实体类转换
     *
     * @param tMMcCouponInfo 卡券类卡券类营销活动列表查询使用实体
     * @return cc.rengu.igas.momp.facade.bean.McListWithDetialInfoBean 卡券类营销活动列表查询接口应答对象
     * @author liujinan
     * @date 2021/1/11 20:35
     */
    private McListWithDetialInfoBean convertCouponMcListInfo(TMMcCouponInfo tMMcCouponInfo) {

        McListWithDetialInfoBean mcListWithDetialInfoBean = new McListWithDetialInfoBean();

        CouponMcInfoBean couponMcInfoBean = new CouponMcInfoBean();

        /* 营销活动编号 */
        couponMcInfoBean.setMcNo(tMMcCouponInfo.getMcNo());
        /* 营销活动名称 */
        couponMcInfoBean.setMcName(tMMcCouponInfo.getMcName());
        /* 营销活动类型 */
        couponMcInfoBean.setMcType(tMMcCouponInfo.getMcType());
        /* 营销活动状态 */
        couponMcInfoBean.setMcStatus(tMMcCouponInfo.getMcStatus());
        /* 活动起始日期 */
        couponMcInfoBean.setMcStartDate(tMMcCouponInfo.getMcStartDate());
        /* 活动截止日期 */
        couponMcInfoBean.setMcEndDate(tMMcCouponInfo.getMcEndDate());
        /* 活动结束日期 */
        couponMcInfoBean.setMcActualEndDate(tMMcCouponInfo.getMcActualEndDate());
        /* 活动时间标志 */
        couponMcInfoBean.setMcTimeFlag(tMMcCouponInfo.getMcTimeFlag());
        /* 出资方 */
        couponMcInfoBean.setSponsor(tMMcCouponInfo.getSponsor());
        /* 参与次数限制 */
        couponMcInfoBean.setRestriction(tMMcCouponInfo.getRestriction());
        /* 客户范围 */
        couponMcInfoBean.setClientRange(tMMcCouponInfo.getClientRange());
        /* 商户范围 */
        couponMcInfoBean.setMerchantRange(tMMcCouponInfo.getMerchantRange());
        /* 新增商户活动标志 */
        couponMcInfoBean.setNewMchntFlag(tMMcCouponInfo.getNewMchntFlag());
        /* 限制金额下限 */
        couponMcInfoBean.setAmtLimitFloor(tMMcCouponInfo.getAmtLimitFloor());
        /* 限制金额上限 */
        couponMcInfoBean.setAmtLimitCelling(tMMcCouponInfo.getAmtLimitCelling());
        /* 预算充值方式 */
        couponMcInfoBean.setBudgetRechargeMethod(tMMcCouponInfo.getBudgetRechargeMethod());
        /* 预算 */
        couponMcInfoBean.setBankBudget(tMMcCouponInfo.getBankBudget());
        /* 银行出资预算 */
        couponMcInfoBean.setBankBudget(tMMcCouponInfo.getBankBudget());
        /* 预期参与商户数量 */
        couponMcInfoBean.setExpectMchntQty(tMMcCouponInfo.getExpectMchntQty());
        /* 银行出资比例 */
        couponMcInfoBean.setBankBudgetRatio(tMMcCouponInfo.getBankBudgetRatio());
        /* 购买标志 */
        couponMcInfoBean.setPurchaseFlag(tMMcCouponInfo.getBankBudgetRatio());
        /* 卡券生效时间 */
        couponMcInfoBean.setAvailableDate(tMMcCouponInfo.getAvailableDate());
        /* 卡券有效期标志 */
        couponMcInfoBean.setCouponDateFlag(tMMcCouponInfo.getCouponDateFlag());
        /* 卡券有效起始日期 */
        couponMcInfoBean.setCouponStartDate(tMMcCouponInfo.getCouponStartDate());
        /* 卡券有效起始日期 */
        couponMcInfoBean.setCouponEndDate(tMMcCouponInfo.getCouponEndDate());
        /* 卡券相对周期 */
        couponMcInfoBean.setCouponPeriod(tMMcCouponInfo.getCouponPeriod());
        /* 卡券库存 */
        couponMcInfoBean.setCouponInventory(tMMcCouponInfo.getCouponInventory());
        /* 卡券未领取张数 */
        couponMcInfoBean.setCouponRest(tMMcCouponInfo.getCouponEndDate());
        /* 卡券已领取张数 */
        couponMcInfoBean.setCouponClaimed(tMMcCouponInfo.getCouponClaimed());
        /* 卡券已核销张数 */
        couponMcInfoBean.setCouponRedeemed(tMMcCouponInfo.getCouponRedeemed());
        /* 审批状态 */
        couponMcInfoBean.setAuditStatus(tMMcCouponInfo.getAuditStatus());
        /* 创建人 */
        couponMcInfoBean.setOprId(tMMcCouponInfo.getOprId());
        /* 创建时间 */
        couponMcInfoBean.setCreateTime(tMMcCouponInfo.getCreateTime());
        /* 最后修改人 */
        couponMcInfoBean.setLastOprId(tMMcCouponInfo.getLastOprId());
        /* 修改时间 */
        couponMcInfoBean.setUpdateTime(tMMcCouponInfo.getUpdateTime());
        /* 机构编码 */
        couponMcInfoBean.setInstId(tMMcCouponInfo.getInstId());
        /* 审批拒绝原因 */
        couponMcInfoBean.setAuditRefuseReason(tMMcCouponInfo.getAuditRefuseReason());
        /* 发布拒绝原因 */
        couponMcInfoBean.setPublishRefuseReason(tMMcCouponInfo.getPublishRefuseReason());
        /* 备用字段1 */
        couponMcInfoBean.setRemark1(tMMcCouponInfo.getRemark1());
        /* 备用字段2 */
        couponMcInfoBean.setRemark2(tMMcCouponInfo.getRemark2());
        /* 备用字段3 */
        couponMcInfoBean.setRemark3(tMMcCouponInfo.getRemark3());
        /* 备用字段4 */
        couponMcInfoBean.setRemark4(tMMcCouponInfo.getRemark4());
        /* 备用字段5 */
        couponMcInfoBean.setRemark5(tMMcCouponInfo.getRemark5());

        mcListWithDetialInfoBean.setMcInfoObj(couponMcInfoBean);

        return mcListWithDetialInfoBean;
    }

    /**
     * 报文检查
     *
     * @param queryMcListWithDetialInfoRequest 卡券类营销活动列表查询接口请求对象
     * @author liujinan
     * @date 2021/1/11 17:53
     */
    private void messageValidation(QueryMcListWithDetialInfoRequest queryMcListWithDetialInfoRequest) {

        /* 检查交易日期 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查交易时间 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查机构标识 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getHeader().getInstId())) {
            rglog.error("机构标识必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = queryMcListWithDetialInfoRequest.getHeader().getInstId();
        }

        /* 检查渠道标识 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getChannelId())) {
            rglog.error("渠道标识必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalChannelId = queryMcListWithDetialInfoRequest.getChannelId();
        }

        /* 检查交易流水号 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getHeader().getTraceNo())) {
            rglog.error("交易流水号必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查查询类型 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getQueryType())) {
            rglog.error("查询类型必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            queryType = queryMcListWithDetialInfoRequest.getQueryType();
        }

        /* 检查查询主体 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getQueryId())) {
            rglog.error("查询主体必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查查询主体所属机构 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getQueryOrgId())) {
            rglog.error("查询主体所属机构必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            queryOrgId = queryMcListWithDetialInfoRequest.getQueryOrgId();
        }

        /* 检查查询主体角色*/
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getQueryRole())) {
            rglog.error("查询主体角色必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 营销活动类型*/
        if (!StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getMcType())) {
            queryMcType = queryMcListWithDetialInfoRequest.getMcType();
        }

        /* 当前页数 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getPageIndex())) {
            rglog.error("当前页数必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 页显示记录数 */
        if (StringUtil.isNullorEmpty(queryMcListWithDetialInfoRequest.getPageSize())) {
            rglog.error("页显示记录数必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        queryMcNo = queryMcListWithDetialInfoRequest.getMcNo();
        queryMcNanme = queryMcListWithDetialInfoRequest.getMcName();
        queryMcStatus = queryMcListWithDetialInfoRequest.getMcStatus();
        queryAuditStatus = queryMcListWithDetialInfoRequest.getAuditStatus();
    }

    /**
     * 获取营销活动机构信息列表
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcOrgInfo> 营销活动机构信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/1/13 18:36
     */
    private List<TMMcOrgInfo> getAllMcOrgListInfo(String mcNo) throws Exception {

        McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
        return mcOrgInfoMapper.selectMcOrgInfoList(globalInstId, mcNo);
    }

    /**
     * 获取营销活动商户信息列表
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcMchntInfo> 营销活动商户信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/1/13 18:35
     */
    private List<Object> getAllMcMchntListInfo(String mcNo) throws Exception {

        List<Object> tmMcMchntInfoList = new ArrayList<>();
        PageInfo pageInfo = new PageInfo();
        int i = 0;
        pageInfo.setPageIndex(i);
        pageInfo.setPageSize(9999);
        TMMcMchntInfo tmMcMchntInfo = new TMMcMchntInfo();
        tmMcMchntInfo.setInstId(globalInstId);
        tmMcMchntInfo.setMcNo(mcNo);
        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        PageResult tmMcMchntInfoPageInfo = mcMchntInfoMapper.selectMcMchntInfoList(tmMcMchntInfo, pageInfo);
        if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
            tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
        }
        while (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult() && tmMcMchntInfoPageInfo.getResult().size() >= 1) {
            i++;
            pageInfo.setPageIndex(i);
            tmMcMchntInfoPageInfo = mcMchntInfoMapper.selectMcMchntInfoList(tmMcMchntInfo, pageInfo);
            if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
                tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
            }
        }
        return tmMcMchntInfoList;
    }

    /**
     * 获取营销活动用户信息列表
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcUserInfo> 营销活动用户信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/1/13 18:38
     */
    private List<TMMcUserInfo> getAllMcUserListInfo(String mcNo) throws Exception {

        McUserInfoMapper mcUserInfoMapper = new McUserInfoMapperImpl();
        return mcUserInfoMapper.selectMcUserInfoList(globalInstId, mcNo);
    }

    /**
     * 获取营销活动时间信息列表
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcTimeInfo> 营销活动时间信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/1/13 18:38
     */
    private List<TMMcTimeInfo> getAllMcTimeListInfo(String mcNo) throws Exception {

        McTimeInfoMapper mcTimeInfoMapper = new McTimeInfoMapperImpl();
        return mcTimeInfoMapper.selectMcTimeInfoList(globalInstId, mcNo);
    }

    /**
     * 获取营销活动规则信息列表
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcCouponRuleInfo> 卡券类营销活动规则表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/1/13 18:40
     */
    private List<TMMcCouponRuleInfo> getAllMcRuleListInfo(String mcNo) throws Exception {

        McRuleInfoMapper mcRuleInfoMapper = new McRuleInfoMapperImpl();
        return mcRuleInfoMapper.selectCouponRuleInfoList(globalInstId, mcNo);
    }

    /**
     * 获取营销活动支付产品信息列表
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcProductInfo> 营销活动产品信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/1/13 18:41
     */
    private List<TMMcProductInfo> getAllMcProductListInfo(String mcNo) throws Exception {

        McProductInfoMapper mcProductInfoMapper = new McProductInfoMapperImpl();
        return mcProductInfoMapper.selectMcProductInfoList(globalInstId, mcNo);
    }

    /**
     * 营销活动机构信息数据库实体类和接口实体类数据转换
     *
     * @param mcOrgInfo 营销活动机构信息表
     * @return cc.rengu.igas.momp.facade.bean.McOrgInfoBean 营销活动机构信息对象
     * @author liujinan
     * @date 2021/1/13 10:33
     */
    private McOrgInfoBean convertMcOrgInfo(TMMcOrgInfo mcOrgInfo) {
        McOrgInfoBean mcOrgInfoBean = new McOrgInfoBean();

        /* 机构编号 */
        mcOrgInfoBean.setOrgId(mcOrgInfo.getOrgId());
        /* 机构层级 */
        mcOrgInfoBean.setOrgHierarchy(mcOrgInfo.getOrgHierarchy());
        /* 机构名称 */
        mcOrgInfoBean.setOrgName(mcOrgInfo.getOrgName());

        return mcOrgInfoBean;
    }

    /**
     * 营销活动商户信息数据库实体类和接口实体类数据转换
     *
     * @param object 营销活动商户信息表
     * @return cc.rengu.igas.momp.facade.bean.McMchntInfoBean 营销活动商户信息对象
     * @author liujinan
     * @date 2021/1/13 10:33
     */
    private McMchntInfoBean convertMcMchntInfo(Object object) {
        McMchntInfoBean mcMchntInfoBean = new McMchntInfoBean();
        TMMcMchntInfo mcMchntInfo = (TMMcMchntInfo) object;
        /* 商户范围类型 */
        mcMchntInfoBean.setMchntType(mcMchntInfo.getMchntType());
        /* 商户范围号 */
        mcMchntInfoBean.setMchntNo(mcMchntInfo.getMchntNo());
        /* 商户范围名称 */
        mcMchntInfoBean.setMchntName(mcMchntInfo.getMchntName());
        /* 新增商户标志 */
        mcMchntInfoBean.setNewMchntFlag(mcMchntInfo.getNewMchntFlag());
        /* 退出标志 */
        mcMchntInfoBean.setExitFlag(mcMchntInfo.getExitFlag());
        /* 操作类型 */
        if ((CommonConstant.MCHNT_REFUSE_FLAG.equals(mcMchntInfo.getNewMchntFlag()) || CommonConstant.MCHNT_NEW_FLAG.equals(mcMchntInfo.getNewMchntFlag())) && CommonConstant.MC_EXIT_FLAG.equals(mcMchntInfo.getExitFlag())) {
            mcMchntInfoBean.setOprType(CommonConstant.BIZ_OPR_INSERT);
        } else {
            mcMchntInfoBean.setOprType(CommonConstant.EMPTY_STRING);
        }

        return mcMchntInfoBean;
    }

    /**
     * 营销活动用户规则信息数据库实体类和接口实体类数据转换
     *
     * @param mcUserInfo 营销活动用户信息表
     * @return cc.rengu.igas.momp.facade.bean.McUserInfoBean 营销活动用户信息对象
     * @author liujinan
     * @date 2021/1/13 10:33
     */
    private McUserInfoBean convertMcUserInfo(TMMcUserInfo mcUserInfo) {
        McUserInfoBean mcUserInfoBean = new McUserInfoBean();
        /* 账户类型 */
        mcUserInfoBean.setAcctType(mcUserInfo.getAcctType());
        /* 账户 */
        mcUserInfoBean.setAcctNo(mcUserInfo.getAcctNo());
        /* 会员ID */
        mcUserInfoBean.setMemberId(mcUserInfo.getMemberId());
        /* 每日限制优惠次数 */
        mcUserInfoBean.setDayRestriction(mcUserInfo.getDayRestriction());
        /* 每周限制优惠次数 */
        mcUserInfoBean.setWeekRestriction(mcUserInfo.getWeekRestriction());
        /* 每月限制优惠次数 */
        mcUserInfoBean.setMonthRestriction(mcUserInfo.getMonthRestriction());
        /* 每季度限制优惠次数 */
        mcUserInfoBean.setQuerterRestriction(mcUserInfo.getQuerterRestriction());
        /* 每年限制优惠次数 */
        mcUserInfoBean.setYearRestriction(mcUserInfo.getYearRestriction());
        /* 活动期间优惠次数限制 */
        mcUserInfoBean.setRestriction(mcUserInfo.getRestriction());

        return mcUserInfoBean;
    }

    /**
     * 营销活动时间规则信息数据库实体类和接口实体类数据转换
     *
     * @param mcTimeInfo 营销活动时间信息表
     * @return cc.rengu.igas.momp.facade.bean.McTimeInfoBean 营销活动事件对象
     * @author liujinan
     * @date 2021/1/13 10:32
     */
    private McTimeInfoBean convertMcTimeInfo(TMMcTimeInfo mcTimeInfo) {
        McTimeInfoBean mcTimeInfoBean1 = new McTimeInfoBean();
        /* 顺序号 */
        mcTimeInfoBean1.setRuleIndex(mcTimeInfo.getRuleIndex());
        /* 星期一日期标志 */
        mcTimeInfoBean1.setFlagMonday(mcTimeInfo.getFlagMonday());
        /* 星期二日期标志 */
        mcTimeInfoBean1.setFlagTuesday(mcTimeInfo.getFlagTuesday());
        /* 星期三日期标志 */
        mcTimeInfoBean1.setFlagWednesday(mcTimeInfo.getFlagWednesday());
        /* 星期四日期标志 */
        mcTimeInfoBean1.setFlagThursday(mcTimeInfo.getFlagThursday());
        /* 星期五日期标志 */
        mcTimeInfoBean1.setFlagFriday(mcTimeInfo.getFlagFriday());
        /* 星期六日期标志 */
        mcTimeInfoBean1.setFlagSaturday(mcTimeInfo.getFlagSaturday());
        /* 星期日日期标志 */
        mcTimeInfoBean1.setFlagSunday(mcTimeInfo.getFlagSunday());
        /* 起始时间 */
        mcTimeInfoBean1.setStartTime(mcTimeInfo.getStartTime());
        /* 结束时间 */
        mcTimeInfoBean1.setEndTime(mcTimeInfo.getEndTime());

        return mcTimeInfoBean1;
    }

    /**
     * 营销活动规则信息数据库实体类和接口实体类数据转换
     *
     * @param mcCouponRuleInfo 卡券类营销活动规则表
     * @return cc.rengu.igas.momp.facade.bean.CouponRuleInfoBean 卡券类营销活动规则信息对象
     * @author liujinan
     * @date 2021/1/13 10:32
     */
    private CouponRuleInfoBean convertMcRuleInfo(TMMcCouponRuleInfo mcCouponRuleInfo) {

        CouponRuleInfoBean couponRuleInfoBean = new CouponRuleInfoBean();

        /* 顺序号  */
        couponRuleInfoBean.setRuleIndex(mcCouponRuleInfo.getRuleIndex());
        /* 活动门槛 */
        couponRuleInfoBean.setCriteria(mcCouponRuleInfo.getCriteria());
        /* 卡券面额 */
        couponRuleInfoBean.setDenomination(mcCouponRuleInfo.getDenomination());
        /* 卡券数量 */
        couponRuleInfoBean.setQuantity(mcCouponRuleInfo.getQuantity());
        /* 单日核销数量 */
        couponRuleInfoBean.setDayRedeemLimit(mcCouponRuleInfo.getDayRedeemLimit());
        /* 购买标志 */
        couponRuleInfoBean.setPurchaseFlag(mcCouponRuleInfo.getPurchaseFlag());
        /* 购买金额 */
        couponRuleInfoBean.setPurchaseAmt(mcCouponRuleInfo.getPurchaseAmt());
        /* 批量任务状态 */
        couponRuleInfoBean.setBatchTaskStatus(mcCouponRuleInfo.getBatchTaskStatus());

        return couponRuleInfoBean;
    }

    /**
     * 营销活动支付产品信息数据库实体类和接口实体类数据转换
     *
     * @param mcProductInfo 营销活动产品信息表
     * @return cc.rengu.igas.momp.facade.bean.McProductInfoBean 营销活动产品对象
     * @author liujinan
     * @date 2021/1/13 10:32
     */
    private McProductInfoBean convertMcProductInfo(TMMcProductInfo mcProductInfo) {
        McProductInfoBean mcProductInfoBean = new McProductInfoBean();
        /* 业务产品代码 */
        mcProductInfoBean.setBizProdCode(mcProductInfo.getBizProdCode());

        return mcProductInfoBean;
    }
}